<?php

namespace Portal\ProfileBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ProfileController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('PortalProfileBundle:Default:index.html.twig', array('name' => $name));
    }
    
    public function myAction()
    {
      $user = self::checkUser($this->get('security.context'));      
      return $this->render('PortalProfileBundle:Profile:index.html.twig', array('name' => $user->getFirstname()));
    }

    public function myLessonAction()
    {
      $user = self::checkUser($this->get('security.context'));
      $lessonArray = $this->getDoctrine()->getEntityManager()->getRepository('PortalLessonBundle:Lesson')->getMyLesson($user->getId());
      
      return $this->render('PortalProfileBundle:Profile:lesson.html.twig', array(
          'name' => $user->getFirstname(),
          'lessonArray' => $lessonArray,
          ));
    }
    

    public function myContractAction()
    {
      $user = self::checkUser($this->get('security.context'));      
      return $this->render('PortalProfileBundle:Profile:contract.html.twig', array('name' => $user->getFirstname()));
    }
    
    public function myInvoiceAction()
    {
      $user = self::checkUser($this->get('security.context'));
      return $this->render('PortalProfileBundle:Profile:invoice.html.twig', array('name' => $user->getFirstname()));
    }
    
    public static function checkUser($securityContext)
    {
      $user = $securityContext->getToken()->getUser();
      
      if ($user) {
        if (!is_object($user)) {          
          header('Location: /logout');
          die;
        }

        if (is_array($user->getRoles()) == FALSE) {
          header('Location: /logout');
          die;
        }

        return $user;
      } else {
        header('Location: /logout');
        die;
      }
    }
    
}
